package com.example.administrator.kaohedemo.View;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.administrator.kaohedemo.Action.Actions;
import com.example.administrator.kaohedemo.Action.CourseListAction;
import com.example.administrator.kaohedemo.R;
import com.example.administrator.kaohedemo.Utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/22.
 */
public class TagerLinearLayout extends LinearLayout implements View.OnClickListener{

    private List<String> list=new ArrayList<String>();
    private Context context;
    private CourseListAction courseListAction;
    private int currentPos=-1;

    public TagerLinearLayout(Context context) {
        super(context);
        init(context);
    }

    public TagerLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public TagerLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        courseListAction= CourseListAction.getInstense();
        this.context=context;
    }

    public void setTagerList(List<String> list){
        this.list=list;
        if (list==null){
            throw new IllegalArgumentException("list must not be null");
        }
        for (int i=0;i<list.size();i++){
            RelativeLayout view= (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.tager_item,null);
            RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(0,0);
            params.width= Utils.getScreenWidth(context)/list.size();
            params.height=ViewGroup.LayoutParams.MATCH_PARENT;
            view.setLayoutParams(params);
            TextView text= (TextView) view.getChildAt(0);
            text.setText(list.get(i));
            view.setTag(i);
            view.setFocusable(true);
            view.setOnClickListener(this);
            this.addView(view);
        }
    }

    public void setDestorySelect(){
        currentPos=-1;
        for (int i = 0; i < list.size(); i++) {
            RelativeLayout layout = (RelativeLayout) getChildAt(i);
            TextView textView = (TextView) layout.getChildAt(0);
            ImageView imageView = (ImageView) layout.getChildAt(1);
            setUnSelect(imageView,textView);
        }
    }

    @Override
    public void onClick(View v) {
        int index=(int) v.getTag();
        if (currentPos!=index) {
            currentPos = index;
            for (int i = 0; i < list.size(); i++) {
                RelativeLayout layout = (RelativeLayout) getChildAt(i);
                TextView textView = (TextView) layout.getChildAt(0);
                ImageView imageView = (ImageView) layout.getChildAt(1);

                if (i == currentPos) {
                    setSelect(imageView,textView);
                } else {
                    setUnSelect(imageView,textView);
                }
            }
            courseListAction.SendItemClick(Actions.CourseListActions.TYPE_TAGER_ONCLICK, index);
        }else {
            currentPos=-1;
            for (int i = 0; i < list.size(); i++) {
                RelativeLayout layout = (RelativeLayout) getChildAt(i);
                TextView textView = (TextView) layout.getChildAt(0);
                ImageView imageView = (ImageView) layout.getChildAt(1);
                setUnSelect(imageView,textView);
            }
            courseListAction.SendItemDestoryClick();
        }
    }
    private void setSelect(ImageView imageView,TextView textView){
        imageView.setImageResource(R.drawable.arrow_down);
        textView.setTextColor(context.getResources().getColor(R.color.color_ff2c97dd));
    }

    private void setUnSelect(ImageView imageView,TextView textView){
        imageView.setImageResource(R.drawable.arrow_up);
        textView.setTextColor(context.getResources().getColor(R.color.color_black));
    }
}
